/*
 * logger.cpp
 *
 *  Created on: Mar 14, 2013
 *      Author: nick
 */


#include "../h/Logger.h"
#include "../h/Destination.h"
#include <cstdlib>

Logging::Logger* Logging::Logger::logger = 0;

void Logging::Logger::removeInstance() {
	delete logger;
}

Logging::Logger & Logging::Logger::getInstance() {
	if(logger == 0) {
		logger = new Logger();
	}
	return *logger;
}

void Logging::Logger::log(const LogMessage & message) const {
	if(message.getSeverityLevel() >= this->severityLevel)
		this->destination->send(message);
}

void Logging::Logger::setDestination(Destination * destination) {
	this->destination = destination;
}

Logging::Logger::Logger() : destination(new LocalDestination()), severityLevel(INFO)  {
	atexit(&Logger::removeInstance);
}

void Logging::Logger::setSeverityLevel(Logging::Level severityLevel) {
	this->severityLevel = severityLevel;
}

Logging::Logger::~Logger() {
	delete destination;
}

bool Logging::Logger::setup(const char * configurationFile) {
	throw new NotImplementedException();
}

void Logging::debug(const std::string & text) {
	Logger::getInstance().log(LogMessage(text, DEBUG));
}

void Logging::info(const std::string & text) {
	Logger::getInstance().log(LogMessage(text, INFO));
}

void Logging::warn(const std::string & text) {
	Logger::getInstance().log(LogMessage(text, WARN));
}

void Logging::error(const std::string & text) {
	Logger::getInstance().log(LogMessage(text, ERROR));
}
